package com.ninong.ker.admin.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.domain.Dept;
import com.ninong.ker.admin.service.DeptService;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 系统部门
 * 
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/dept")
public class DeptResouces {

	@Autowired
	private DeptService deptService;

	@Log("部门列表")
	@CmdAdmin({ "admin", "dept:list" })
	@PostMapping
	public ResponseEntity<CmdResponse> list(@RequestBody Dept entity) {
		CmdResponse response = new CmdResponse();
		response.setData(deptService.listDeptTree(entity));
		return ResponseEntity.ok(response);
	}

	@Log("部门删除")
	@CmdAdmin({ "admin", "dept：delete" })
	@DeleteMapping
	public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
		CmdResponse response = new CmdResponse();
		deptService.delete(ids);
		response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
		return ResponseEntity.ok(response);
	}

	@Log("部门新增或编辑")
	@CmdAdmin({ "admin", "dept:add", "dept:update" })
	@PutMapping
	public ResponseEntity<CmdResponse> create(@RequestBody Dept entity) {
		deptService.create(entity);
		return ResponseEntity.ok(new CmdResponse());
	}

	@Log("部门详情")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CmdResponse> get(@PathVariable String id) {
		CmdResponse response = new CmdResponse();
		response.setData(deptService.get(id));
		return ResponseEntity.ok(response);
	}

}
